package com.tledu.zyf.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.tledu.zyf.dao.BaseDao;
import com.tledu.zyf.dao.IUserDao;
import com.tledu.zyf.model.User;
import com.tledu.zyf.util.MyBatisUtil;
import com.tledu.zyf.util.Pager;

public class UserDao extends BaseDao<User> implements IUserDao {

	@Override
	public User loadByUsername(String username) {
		return loadByUsername(User.class.getName() + ".loadByUsername", username);
	}

	public User loadByUsername(String name, String username) {
		SqlSession session = null;
		User user = null;
		try {
			session = MyBatisUtil.getSession();
			user = session.selectOne(name, username);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSession(session);
		}
		return user;
	}

	@Override
	public List<User> list() {
		return list(User.class);
	}

	@Override
	public void delete(int id) {
		delete(User.class, id);
	}

	@Override
	public User load(int id) {
		return load(User.class, id);
	}

	@Override
	public List<User> vagueUsername(String username) {
		return vagueUsername(User.class.getName() + ".vagueUsername", username);
	}

	public List<User> vagueUsername(String name, String username) {
		SqlSession session = null;
		List<User> users = null;
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			session = MyBatisUtil.getSession();
			params.put("username", "%" + username + "%");
			users = session.selectList(name, params);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSession(session);
		}
		return users;
	}

	@Override
	public int loadByGroupId(int groupId) {
		return loadByGroupId(User.class.getName() + ".loadByGroupId", groupId);
	}

	public int loadByGroupId(String name, int groupId) {
		SqlSession session = null;
		int count = 0;
		try {
			session = MyBatisUtil.getSession();
			count = session.selectOne(name, groupId);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSession(session);
		}
		return count;

	}

	@Override
	public Pager<User> find(int page, int limit) {
		return find(User.class.getName() + ".find", page, limit);
	}

	public Pager<User> find(String name, int page, int limit) {
		Pager<User> pager = new Pager<User>();
		List<User> users = new ArrayList<User>();

		// 存放传入的两个参数
		Map<String, Object> params = new HashMap<String, Object>();

		SqlSession session = null;
		try {
			session = MyBatisUtil.getSession();
			// 设置传入的两个参数
			if (page == 0) {
				// 如果页码为 0 ,那么起始值就是0
				params.put("pageOffset", 0);
				params.put("pageSize", limit);
			} else {
				params.put("pageOffset", (page - 1) * limit);
				params.put("pageSize", limit);
			}
			users = session.selectList(name, params);
			session.commit();

			// 把 list 设置到 pager 中
			pager.setData(users);

			// 查询总条数 返回一个 int 类型的值 直接设置到 pager 中
			int count = session.selectOne(name + "_count");
			pager.setCount(count);
			session.commit();
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSession(session);
		}
		return null;
	}

}
